﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class UserControl_Industry : System.Web.UI.UserControl
{
    List<int> _industryIDList = new List<int>();
    public List<int> Select
    {
        get
        {
            if (ViewState["industry"] != null)
            {
                return (List<int>)ViewState["industry"];
            }
            else
            {
                return null;
            }
        }
        set
        {
            _industryIDList = value;
            if (ViewState["industry"] != null)
            {
                ViewState["industry"] = _industryIDList;
            }
            else
            {
                ViewState.Add("industry", _industryIDList);
            }

            CheckBoxList1.DataBind();
            using(var _adc=DBHelper.GetDataContext())
            {
            foreach (ListItem _item in CheckBoxList1.Items)
            {
                foreach (var _i in _industryIDList)
                {
                    if (_item.Value == _i.ToString())
                    {
                        _item.Selected = true;
                        lbl_Select.Text += _adc.Industry.FirstOrDefault(i => i.ID == _i).Name + " ";
                    }
                }
            }
            }
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        lbl_Select.Text="";
        using (var _adc = DBHelper.GetDataContext())
        {
            foreach (ListItem _item in CheckBoxList1.Items)
            {
                if (_item.Selected == true)
                {
                    lbl_Select.Text += _item.Text + "&nbsp;&nbsp;&nbsp;";
                    _industryIDList.Add(Convert.ToInt32(_item.Value));
                }
            }
        }
        if(ViewState["industry"]==null)
        {
            ViewState.Add("industry",_industryIDList);
        }
        else
        {
            ViewState["industry"]=_industryIDList;
        }
    }
}
